package com.yangzhe.happyreader;

import android.app.Application;

import com.yangzhe.base.AppContext;
import com.yangzhe.base.ApplicationAsLibrary;
import com.yangzhe.interview.InterViewAPP;
import com.yangzhe.okhttputils.OkHttpUtils;
import com.yangzhe.okhttputils.common.RequestHeaders;
import com.yangzhe.okhttputils.common.RequestParams;
import com.yangzhe.recreation.RecreationAPP;
import com.yangzhe.video.ReaderApp;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by erfli on 11/1/16.
 */
public class ModularizationApplication extends Application {
    private List<ApplicationAsLibrary> mChildApplicationList = new ArrayList<>();
    public static boolean isDebug = true;

    @Override
    public void onCreate() {
        super.onCreate();
        AppContext.init(getApplicationContext());
        mChildApplicationList.add(new ReaderApp());
        mChildApplicationList.add(new RecreationAPP());
        mChildApplicationList.add(new InterViewAPP());

        initJPush();
        initOkhttpClient();
        for (ApplicationAsLibrary app : mChildApplicationList) {
            app.onCreateAsLibrary(this);
        }


    }

    private void initJPush() {
        JPushInterface.init(this);
        JPushInterface.setDebugMode(true); // 设置开启 JPush 日志,发布时请关闭日志
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        for (ApplicationAsLibrary app : mChildApplicationList) {
            app.onTrimMemoryAsLibrary(this, level);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        for (ApplicationAsLibrary app : mChildApplicationList) {
            app.onLowMemoryAsLibrary(this);
        }
    }

    private void initOkhttpClient() {

        RequestHeaders headers = new RequestHeaders();
        headers.put("aaa", "111");

        RequestParams params = new RequestParams();
        params.put("bbb", "222");


        OkHttpUtils.getInstance()
                .debug(isDebug, "myokhttp")
                .setConnectTimeout(OkHttpUtils.DEFAULT_MILLISECONDS)//全局的连接超时时间
                .setReadTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS)//全局的读取超时时间
                .setWriteTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS)//全局的写入超时时间
                .addCommonHeader(headers)
                .addCommonParams(params);

    }

}
